﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SaleRegister.aspx.cs" Culture="en-GB"
    Inherits="Main_Reports_Accounts_SaleRegister" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="AjaxControl" %>
<%@ Register Src="~/User Controls/PageHeading.ascx" TagName="heading" TagPrefix="acc" %>
<%@ Register Src="~/User Controls/Pager.ascx" TagPrefix="custom" TagName="Pager" %>
<%@ Register Src="~/User Controls/CommonControls.ascx" TagName="CommonControls" TagPrefix="MyControls" %>
<%@ Register Assembly="CalendarExtenderPlus" Namespace="AjaxControlToolkitPlus" TagPrefix="advancedCalender" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Sale Invoice DayBook</title>
    <link href="../../../Styles/accounts.css" rel="stylesheet" type="text/css" />

    <script type="text/javascript" language="javascript">
        function checkdate() {
            if (document.getElementById("txtfrom").value == '') {
                alert('Specify From date!');
                document.getElementById("txtfrom").focus();
                return false;
            }
            else if (document.getElementById("txtto").value == '') {
                alert('Specify To Date!');
                document.getElementById("txtto").focus();
                return false;
            }
            else {
                var strFromDate = document.getElementById("txtfrom").value;
                var strToDate = document.getElementById("txtto").value;
                var dtFrom = parseInt(strFromDate.substring(0, 2), 10);
                var monFrom = parseInt(strFromDate.substring(3, 5), 10);
                var yrFrom = parseInt(strFromDate.substring(6, 10), 10);
                var dtTo = parseInt(strToDate.substring(0, 2), 10);
                var monTo = parseInt(strToDate.substring(3, 5), 10);
                var yrTo = parseInt(strToDate.substring(6, 10), 10);
                var dateFrom = new Date(yrFrom, monFrom, dtFrom);
                var dateTo = new Date(yrTo, monTo, dtTo);
                if (dateTo < dateFrom) {
                    alert("from date cannot be greater than to date");
                    document.getElementById("txtfrom").focus();
                    return false;
                }
                else {
                    return true;
                }
            }
        }
        function EnabledTabKey(id, e) {
            var keycode;
            if (window.event) {
                keycode = window.event.keyCode;
            }
            else if (e) {
                keycode = e.which;
            }
            else {
                return true;
            }

            if (keycode == 9) {
                // if is the tab key                
                return true;
            }
            else {
                return false;
            }
        }
        function Validation() {
            var flag = true;
            var txtFromdate = document.getElementById("txtFromDate").id;
            var txtTodate = document.getElementById("txtToDate").id;
            if (document.getElementById("txtFromDate").value == '') {
                alert('Specify From date');
                flag = false;
            }
            else if (document.getElementById("txtToDate").value == '') {
                alert('Specify To date');
                flag = false;
            }
            else {
                if (isDate(txtFromdate) && isDate(txtTodate)) {
                    var dateArray = new Array();
                    dateArray = document.getElementById("txtFromDate").value.split("/");
                    var day1 = dateArray[0];
                    var month1 = dateArray[1];
                    var year1 = dateArray[2];
                    dateArray = document.getElementById("txtToDate").value.split("/");
                    var day2 = dateArray[0];
                    var month2 = dateArray[2];
                    var year2 = dateArray[2];

                    if (day1.length < 2) {
                        day1 = '0' + day1;
                    }
                    if (month1.length < 2) {
                        month1 = '0' + month1;
                    }
                    if (day2.length < 2) {
                        day2 = '0' + day2;
                    }
                    if (month2.length < 2) {
                        month2 = '0' + month2;
                    }
                    var date1 = new Date(year1, month1 - 1, day1);
                    var date2 = new Date(year2, month2 - 1, day2);
                    if (date1 > date2) {
                        alert('Date Could Not be less than From Date');
                        document.getElementById(txtFromdate).focus();
                        flag = false;

                    }
                }
                else {
                    flag = false;
                }
            }
            return flag;
        }
        function isDate(txtDateId) {
            // date length should be 10 characters - no more, no less            
            var Ctrl = document.getElementById(txtDateId);
            var txtDate = Ctrl.value;
            var flag = true;

            // extract day, month and year from the txtDate string
            // expected format is dd/mm/yyyy
            var day = '';
            var month = '';
            var year = '';
            var dateArray = new Array();
            var count = 0;
            for (var j = 0; j < txtDate.length; j++) {
                var chara = txtDate.charAt(j);
                if (chara == '/') {
                    count += 1;
                }
            }
            if (count != 2) {
                alert('Invalid date !');
                return false;
            }
            dateArray = txtDate.split("/");


            day = dateArray[0];
            month = dateArray[1];
            year = dateArray[2];


            if (year.length != 4) {
                alert('Invalid date !');
                return false;
            }
            var finYear = document.getElementById("hidFinyear").value;
            if (month >= 4 && year != parseInt(finYear)) {
                alert('Date Should be between 01/04/' + finYear + ' and 31/03/' + (parseInt(finYear) + 1));
                return false;
            }
            else if (month < 4 && year != (parseInt(finYear) + 1)) {
                alert('Date Should be between 01/04/' + finYear + ' and 31/03/' + (parseInt(finYear) + 1));
                return false;
            }



            if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
                if (day > 31) {
                    alert('Date Should not be Greater than 31');
                    Ctrl.focus();
                    flag = false;
                }
            }
            //for have only 30 day
            else if (month == 4 || month == 6 || month == 9 || month == 11) {
                if (day > 31) {
                    alert('Date Should not be Greater than 30');
                    Ctrl.focus();
                    flag = false;
                }
            }
            //for Febuary 
            else if (month == 2) {
                var leap = year % 4;
                if (leap == 0) {
                    if (day > 29) {
                        alert('february have only 29 days');
                        Ctrl.focus();
                        flag = false;
                    }
                }
                else {
                    if (day > 28) {
                        alert('february have only 28 days');
                        Ctrl.focus();
                        flag = false;
                    }
                }
            }
            else if (month > 12) {
                alert('Invalid Date');
                Ctrl.focus();
                flag = false;
            }
            // otherwise return true
            return flag;
        }

        //        function printGrid() {
        //            //open new window set the height and width =0,set windows position at bottom
        //            var a = window.open('about:blank', 'SaleTransactions', 'left =0,top=0,width=500,height=300,toolbar=0,scrollbars=0,status=0');
        //            //write gridview data into newly open window
        ////            a.document.write(document.getElementById('<%= grdDisplay.ClientID %>').innerHTML);
        ////            a.document.close();
        ////            a.focus();
        ////            //call print
        ////            a.print();
        ////            a.close();
        //            //            return false;
        //            a.document.write(document.getElementById('prnt').innerHTML);
        //            a.document.close();
        //            a.focus();
        //            //call print
        //            a.print();
        //            a.close();
        //            return false;
        //        }
        function printGrid() {
            var fromDate = document.getElementById("hidFromDate").value;
            var toDate = document.getElementById("hidToDate").value;
            var showopt = document.getElementById("hidShowOpt").value;
            if (fromDate != '' && toDate != '' && showopt != '') {
                window.open('SaleRegisterPrint.aspx?from=' + fromDate + '&to=' + toDate+'&showopt='+showopt, '', 'height=600,width=800,scrollbars=1');
            }
            return false
        }

    </script>

    <style type="text/css" media="print">
        #grdDisplay tr
        {
            page-break-inside: avoid;
            page-break-after: auto;
        }
        #grdDisplay thead
        {
            display: table-header-group;
        }
        #grdDisplay tfoot
        {
            display: table-footer-group;
        }
        .borderToRow
        {
            border: solid 1px #818181;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="scriptmanager1" runat="server">
    </asp:ScriptManager>
    <acc:heading ID="pagehead" runat="server" HeadingText="Sale Register" />
    <div style="width: 90%; margin-left: 5%; margin-right: 5%">
        <div>
            <asp:LinkButton ID="lnkChngDate" runat="server" Text="Change Date"></asp:LinkButton>&nbsp;&nbsp;<asp:LinkButton
                ID="lnkprint" runat="server" Text="Print" OnClientClick="return printGrid();"></asp:LinkButton>
            <center>
                <b>
                    <asp:Label ID="lblMsg" runat="server"></asp:Label></b></center><br />
            <asp:HiddenField ID="hidFinyear" runat="server" />
            <asp:HiddenField ID="hidFromDate" runat="server" />
            <asp:HiddenField ID="hidToDate" runat="server" />
            <asp:HiddenField ID="hidShowOpt" runat="server" />
        </div>
        <asp:Panel ID="pnlGrid" CssClass="tablecontent" runat="server">
            <div id="divprint" runat="server">
                <div>
                    <table width="100%">
                        <tr>
                            <td>
                                <table cellpadding="0" cellspacing="0">
                                    <tr>
                                        <td>
                                            <span style="padding-left: 0px;">Show Option</span>
                                        </td>
                                        <td>
                                            <asp:RadioButtonList ID="rdbtnlst1" runat="server" RepeatDirection="Horizontal" AutoPostBack="true"
                                                RepeatLayout="Table" OnSelectedIndexChanged="rdbtnlst1_SelectedIndexChanged">
                                                <asp:ListItem Value="0" Text="Debit&nbsp;&nbsp;&nbsp;&nbsp;"></asp:ListItem>
                                                <asp:ListItem Value="1" Text="Credit&nbsp;&nbsp;&nbsp;&nbsp;"></asp:ListItem>
                                                <asp:ListItem Value="2" Text="Both&nbsp;&nbsp;&nbsp;&nbsp;" Selected="True"></asp:ListItem>
                                            </asp:RadioButtonList>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                            <td align="right">
                                <custom:Pager ID="custPager" runat="server" OnPageChanged="custPager_PageChanged" />
                            </td>
                            <td align="left" style="text-align: left;">
                                <MyControls:CommonControls ID="CommonControls2" runat="server" GridviewControlId="grdDisplay"
                                    HideColumns="" />
                            </td>
                        </tr>
                    </table>
                </div>
                <div id="prnt">
                    <asp:GridView ID="grdDisplay" Width="100%" runat="server" AutoGenerateColumns="false"
                        CssClass="gridview" AllowPaging="true" PageSize="20" PagerStyle-CssClass="pgr"
                        ShowFooter="true" OnRowDataBound="grdDisplay_RowDataBound"
                        OnPageIndexChanging="grdDisplay_OnPageIndexChanging">
                        <Columns>
                            <asp:TemplateField HeaderText="S.No." ItemStyle-HorizontalAlign="Center">
                                <ItemTemplate>
                                    <%--<%# Container.DataItemIndex+1 %>--%>
                                    <asp:Label ID="lblsno" runat="server"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField HeaderText="Date" DataField="TransactionDate" DataFormatString="{0:dd-MMM-yyyy}" />
                            <asp:BoundField HeaderText="Transaction Type" DataField="Name" />
                            <asp:BoundField HeaderText="Voucher No" DataField="transactionnumber" />
                            <asp:BoundField HeaderText="Particulars" DataField="Particulars" />
                            <asp:TemplateField HeaderText="Debit">
                                <ItemStyle HorizontalAlign="Right" />
                                <ItemTemplate>
                                    <asp:Label ID="lblDebit" runat="server"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Credit">
                                <ItemStyle HorizontalAlign="Right" />
                                <ItemTemplate>
                                    <asp:Label ID="lblCredit" runat="server"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <EmptyDataRowStyle ForeColor="Red" Font-Bold="true" HorizontalAlign="Center" />
                        <EmptyDataTemplate>
                            Record Not Found !
                        </EmptyDataTemplate>
                    </asp:GridView>
                </div>
            </div>
        </asp:Panel>
        <br />
    </div>
    <asp:Panel ID="PanelChangeDate" runat="server" Width="350px" Height="160px" Style="display: none"
        CssClass="modalPopup">
        <div style="width: 80%; margin: 20px 10% 10px 10%;">
            <table>
                <tr>
                    <td>
                        From date
                    </td>
                    <td style="padding-left: 10px">
                        <asp:TextBox ID="txtFromDate" runat="server" CssClass="input" ToolTip="DD/MM/YYYY"
                            Width="100px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        To Date
                    </td>
                    <td style="padding-left: 10px">
                        <asp:TextBox ID="txtToDate" runat="server" CssClass="input" ToolTip="DD/MM/YYYY"
                            Width="100px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <asp:Button ID="btnCangeDate" runat="server" CssClass="button" Text="Ok" OnClick="btnCangeDate_Click"
                            OnClientClick="return Validation();" />&nbsp;
                        <asp:Button ID="btncancel" runat="server" CssClass="button" Text="Cancel" />&nbsp;
                    </td>
                </tr>
            </table>
        </div>
    </asp:Panel>
    <ajaxcontrol:modalpopupextender id="modalDate" runat="server" cancelcontrolid="btncancel"
        targetcontrolid="lnkChngDate" popupcontrolid="PanelChangeDate" backgroundcssclass="modalBackground">
    </ajaxcontrol:modalpopupextender>
    </form>
</body>
</html>
